home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / SENDMAILTAG.PY < prev    next >
Encoding:
Python Source  |  2000-06-20  |  7.4 KB  |  195 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __rcs_id__='$Id: SendMailTag.py,v 1.7.38.2 2000/06/20 18:04:55 brian Exp $'
  65. __version__='$Revision: 1.7.38.2 $'[11:-2]
  66.  
  67. from MailHost import MailBase
  68. from DocumentTemplate.DT_Util import *
  69. from DocumentTemplate.DT_String import String
  70. from socket import gethostname
  71. import string
  72.  
  73. class SendMailTag:
  74.     '''the send mail tag, used like thus:
  75.  
  76.     <dtml-sendmail mailhost="someMailHostID">
  77.     to: person@their.machine.com
  78.     from: me@mymachine.net
  79.     subject: just called to say...
  80.  
  81.     boy howdy!
  82.     </dtml-sendmail>
  83.  
  84.     Text between the sendmail and /sendmail tags is processed
  85.     by the MailHost machinery and delivered.  There must be at least
  86.     one blank line seperating the headers (to/from/etc..) from the body
  87.     of the message.
  88.  
  89.     Instead of specifying a MailHost, an smtphost may be specified
  90.     ala 'smtphost="mail.mycompany.com" port=25' (port defaults to 25
  91.     automatically).  Other parameters are
  92.  
  93.     * mailto -- person (or comma-seperated list of persons) to send the
  94.     mail to.  If not specified, there **must** be a to: header in the
  95.     message.
  96.  
  97.     * mailfrom -- person sending the mail (basically who the recipient can
  98.     reply to).  If not specified, there **must** be a from: header in the
  99.     message.
  100.  
  101.     * subject -- optional subject.  If not specified, there **must** be a
  102.     subject: header in the message.
  103.  
  104.     * encode -- optional encoding. Possible values are: 'base64',
  105.      'quoted-printable' and 'uuencode'.
  106.  
  107.     '''
  108.  
  109.     name='sendmail'
  110.     blockContinuations=()
  111.     encode=None
  112.  
  113.     def __init__(self, blocks):
  114.         tname, args, section=blocks[0]
  115.         args=parse_params(args, mailhost=None, mailto=None, mailfrom=None,
  116.                           subject=None, smtphost=None, port='25',
  117.                           encode=None)
  118.  
  119.         for key in ('mailto', 'mailfrom', 'subject', 'smtphost', 'port'):
  120.             if not args.has_key(key):args[key]=''
  121.         smtphost=None
  122.  
  123.         has_key=args.has_key
  124.         if has_key('mailhost'): mailhost=args['mailhost']
  125.         elif has_key('smtphost'): mailhost=smtphost=args['smtphost']
  126.         elif has_key(''): mailhost=args['mailhost']=args['']
  127.         else: raise MailHostError, 'No mailhost was specified in tag'
  128.  
  129.         if has_key('encode') and args['encode'] not in \
  130.         ('base64', 'quoted-printable', 'uuencode', 'x-uuencode',
  131.          'uue', 'x-uue'):
  132.             raise MailHostError, (
  133.                 'An unsupported encoding was specified in tag')
  134.  
  135.         if not smtphost:
  136.             self.__name__=self.mailhost=mailhost
  137.             self.smtphost=None
  138.         else:
  139.             self.__name__=self.smtphost=smtphost
  140.             self.mailhost=None
  141.         self.section=section
  142.         self.args=args
  143.         self.mailto=args['mailto']
  144.         self.mailfrom=args['mailfrom']
  145.         self.subject=None or args['subject']
  146.         if args['port'] and type(args['port']) is type('s'):
  147.             self.port=args['port']=string.atoi(args['port'])
  148.         elif args['port']=='':
  149.             self.port=args['port']=25
  150.         else:
  151.             self.port=args['port']
  152.         if has_key('encode'):
  153.             self.encode=args['encode']
  154.         else: self.encode=None
  155.  
  156.     def render(self, md):
  157.         args=self.args
  158.         has_key=args.has_key
  159.  
  160.         if self.mailhost:
  161.             mhost=md[self.mailhost]
  162.         elif self.smtphost:
  163.             mhost=MailBase()
  164.             mhost._init(self.smtphost, self.port)
  165.  
  166.         mhost.send(self.section(md.this, md), self.mailto, self.mailfrom,
  167.                    self.subject, self.encode)
  168.  
  169.         return ' '
  170.  
  171.     __call__=render
  172.  
  173. String.commands['sendmail']=SendMailTag
  174.